<?php
class dbloader {
  function add($label,$CFG) {
    $result = false;
    //label should contain an unique identifier
    /**
      CFG should contain indexes for Parameters required to create a DB Connection
	* dbhost
	* dbname
	* dbuser
	* dbpass
     **/
    switch ($CFG{'dbtype'}) {
    case "mysql":
    $conn = mysql_connect($CFG{'dbhost'}, $CFG{'dbuser'}, $CFG{'dbpass'});
    break;
    case "mysqli":
    $conn = mysqli_connect($CFG{'dbhost'}, $CFG{'dbuser'}, $CFG{'dbpass'},$CFG{'dbname'});
    break;
    }
    if ($conn) {
      //connection was successful, add to class
      $Tcfg = array();
      $Tcfg['dbname'] = $CFG{'dbname'};
      $Tcfg['dbconn'] = $conn;
      $i = $this->dbcount();
      $this->databases[$i] = array('label' =>$label,'config' =>$Tcfg);
      $this->dbindex[$label] = $i;
      $result = true;
    } else {
      $Ecfg = array();
      $Ecfg['msg'] = "could not connect to database.";
      $Ecfg['label'] = $label;
      $this->errors[] = $Ecfg;
    } 
    return $result;
  }
  private function remove($label) {
    //removes a database item based on it's identifier
  }
  function labels() {
    //return an array all of identifier names
    $labels = array();
    $max = count($this->databases);
    $i = 0;
      foreach ($this->databases as $database) {
				$labels[] = $database['label'];
      } 
    return $labels;
  }
  function param($label,$parameter) {
    //returns a parameter value of a given database
    if (isset($this->dbindex[$label])) {
		  if (isset($this->databases[$this->dbindex[$label]]['config'][$parameter])) {
		  	if ($parameter != "password") {
		    	return $this->databases[$this->dbindex[$label]]['config'][$parameter];
		    }
		  } else {
		    return false;
		  } 
    } else {
    	return false;
    }
  }
  function dbcount() {
    return count($this->databases);
  }
  function info() {
  $html = "";
    $html .= "<p>Database Count:".count($this->databases)."</p>\n";
  return $html;
  }
  function print_databases() {
    print_r($this->databases);
  }
  function print_errors() {
    print_r($this->errors);
  }
  function loadDBconfigFile($file,$dblayer) {
    if (file_exists($file)) {
      include($file);
      if (isset($C['databases'])) {
				foreach ($C['databases'] as $database) {
					$this->add($database['label'],$database);
					$dblayer->add_layer($database['label'],$database);
				}
      }
    }
  }
  function loadDBconfig($C) {
    if (isset($C['databases'])) {
	foreach ($C['databases'] as $database) {
	  $this->add($database['label'],$database);
	}
    }
  }
private $databases = array();
private $dbindex = array();
private $errors = array();
}
/** NOTE : Example Usage
$dbs = new dbloader;
$dbs->loadDBconfigFile('template.cfg.php');
$dbs->loadDBconfigFile('template2.cfg.php');
// $dbs->print_databases();
// $dbs->print_errors();
print_r($dbs->labels());
echo "\n".$dbs->param('testdb','dbconn')."\n";**/
?>
